function chattingCtrl($scope)
{
    $scope.tempNickName = "";
    $scope.userInfo = {};

    $scope.isLogin = false;
    $scope.socket = null;
    $scope.rooms = [ {name : "01번방"},{name : "02번방"},{name : "03번방"},{name : "04번방"},{name : "05번방"},{name : "06번방"},{name : "07번방"},{name : "08번방"},{name : "09번방"} ];
    $scope.selectRoom = [$scope.rooms[0]];

    $scope.ReservedBool = function()
    {
        return $scope.isLogin == true ? false : true;
    }
    $scope.JoinRoom = function(user)
    {
        if( $scope.userInfo.nickName != undefined || $scope.tempNickName == "")
        {
            alert('대화명을 입력 해주세요!!!');
            return;
        }

        $scope.isLogin = true;
        $scope.userInfo.nickName = user;

        $scope.socket = io.connect('http://localhost:3000');

        $scope.socket.emit('join', {'userid' : user, "roomname":  $scope.selectRoom.name});
        $scope.socket.on('connected', function(){
            alert('connected');
        });

        $scope.socket.on('join', function(data)
        {

            $scope.socket.on('ping', function(data)
            {
                $scope.socket.emit('pong', {'userid': $scope.userInfo.nickName, "roomname": $scope.selectRoom.name, "msg": "yes"});
            });

            $('#chatLayout').append("<li class='clearfix'><div class='header'><strong class='primary-font'>" +  data + "</strong> 님이 입장 하셨습니다. </div> </li>");
            // alert('join');
            //$('#ulMessage').append("<li>" + data + "</li>");
        });

        $scope.socket.on('out', function(data)
        {



            $('#chatLayout').append("<li class='clearfix'><div class='header'><strong class='primary-font'>" +  data + "</strong> 님이 퇴장 하셨습니다. </div> </li>");
            // alert('join');
            //$('#ulMessage').append("<li>" + data + "</li>");
        });



        $scope.socket.on('message', function(data)
        {

            if(data.msg == "!disconnect!")
            {
                $('#chatLayout').append("<li class='clearfix'><div class='header'><strong class='primary-font'>" +  data.id + "</strong> 님이 퇴장 하셨습니다. </div> </li>");
            }
            else if(data.userid == $scope.userInfo.nickName)
            {

            }
            else
            {

                $('#chatLayout').append("<li class='left clearfix'><span class='chat-img pull-left'>" +
                    "<img class='img-circle' src='http://placehold.it/50/55C1E7/fff&text=U', alt='User Avatar'>" +
                    "</img></span><div class='chat-body clearfix'><div class='header'><strong class='primary-font'>" +  data.userid + "</strong>" +
                    "<small class='pull-right text-muted'><span class='glyphicon glyphicon-time'> 12 mins ago</span></small>" +
                    "</div><p>" + data.msg + "</p></div><li>");

                $($('#chatLayout')[0].lastChild).remove();
                //$('#chatBody').scrollTop($('#chatBody')[0].scrollTopMax);
                $("#chatBody").animate({ scrollTop: $('#chatBody')[0].scrollHeight}, "fast");



            }
        });


    };


    $scope.init = function()
    {

        if($scope.socket == null) return;


    }
}

var keylist="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"
var temp=''

function generatepass(plength){
    temp=''
    for (i=0;i<plength;i++)
    temp+=keylist.charAt(Math.floor(Math.random()*keylist.length))
    return temp
    }

function populateform(enterlength){

    var v = generatepass(enterlength)
    }

function check(){
    if(temp == document.frm.che.value){
    alert('같다');
    }else{
    alert('틀림');
    }
    }



$(window).on('beforeunload', function(){

    var scope = angular.element($("#controllerLayout")).scope();
    scope.$apply(function(){
        scope.socket.emit('disconnect', {'userid': scope.userInfo.nickName, "roomname": scope.selectRoom.name, "msg": 'ㄹㄹㄹ'});
    });
    alert('채팅이 종료 되었습니다.');
});

$(window).unload(function() {
// your logout code goes here
    var scope = angular.element($("#controllerLayout")).scope();
    scope.$apply(function(){
        scope.socket.emit('disconnect', {'userid': scope.userInfo.nickName, "roomname": scope.selectRoom.name, "msg": 'ㄹㄹㄹ'});
    });
        alert('채팅이 종료 되었습니다.');
});

$(document).ready(function ()
{


    $('#messageText').on('keydown', function(e)
    {
        if(e.keyCode == 13){ sendMsg(); return; }
    });
    $("#btn-chat").on('click',function(){

        sendMsg();


    //귓속말
    /*    var scope = angular.element($("#controllerLayout")).scope();
        scope.$apply(function(){
            scope.socket.emit('whisper', {'userid': "2", "roomname": scope.selectRoom.name, "msg": "FFFFF"});
        });*/

    });
    function sendMsg() {
        var msg = $('#messageText').val();
        var scope = angular.element($("#controllerLayout")).scope();
        scope.$apply(function(){
            scope.socket.emit('message', {'userid': scope.userInfo.nickName, "roomname": scope.selectRoom.name, "msg": msg});
        });

        $('#chatLayout').append("<li class='right clearfix'><span class='chat-img pull-right'>" +
            "<img class='img-circle' src='http://placehold.it/50/FA6F57/fff&text=ME', alt='User Avatar'>" +
            "</img></span><div class='chat-body clearfix'><div class='header'><strong class='primary-font'>" +  scope.userInfo.nickName+ "</strong>" +
            "<small class='pull-right text-muted'><span class='glyphicon glyphicon-time'> 12 mins ago</span></small>" +
            "</div><p>" + msg + "</p></div><li>");

        $($('#chatLayout')[0].lastChild).remove();
        $("#chatBody").animate({ scrollTop: $('#chatBody')[0].scrollHeight}, "fast");
        $('#messageText').val("");
    }
});


